package com.wgcloud;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wgcloud.entity.SystemInfo;
import com.wgcloud.httpClient.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

@Slf4j
@SpringBootApplication
// @ComponentScan(basePackages = "com.wgcloud")
//b5e58bd6ffced2985ae27df2a607073b
@EnableCaching
@EnableScheduling
public class WgcloudServiceApplication {
    @Autowired
    private CommonConfig commonConfig;
    @Autowired
    private HttpClientUtil httpClientUtil;


    public static void main(String[] args) {
        SpringApplication.run(WgcloudServiceApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  监控客户端启动成功   ლ(´ڡ`ლ)ﾞ  \n" + " ________  ___  ___  ________  ________  ________  ________      \n" + "|\\   ____\\|\\  \\|\\  \\|\\   __  \\|\\   ____\\|\\   __  \\|\\   ___  \\    \n" + "\\ \\  \\___|\\ \\  \\\\\\  \\ \\  \\|\\  \\ \\  \\___|\\ \\  \\|\\  \\ \\  \\\\ \\  \\   \n" + " \\ \\_____  \\ \\  \\\\\\  \\ \\   ____\\ \\  \\    \\ \\  \\\\\\  \\ \\  \\\\ \\  \\  \n" + "  \\|____|\\  \\ \\  \\\\\\  \\ \\  \\___|\\ \\  \\____\\ \\  \\\\\\  \\ \\  \\\\ \\  \\ \n" + "    ____\\_\\  \\ \\_______\\ \\__\\    \\ \\_______\\ \\_______\\ \\__\\\\ \\__\\\n" + "   |\\_________\\|_______|\\|__|     \\|_______|\\|_______|\\|__| \\|__|\n" + "   \\|_________|                                                  \n");
    }

    @Bean
    public RestTemplate restTemplate() {
        StringHttpMessageConverter m = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        return new RestTemplateBuilder().additionalMessageConverters(m).build();
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        return taskScheduler;
    }

    @Bean // CommandLineRunner用于在 Spring Boot 应用程序启动后执行一些初始化任务
    public CommandLineRunner printMessage() {
        return args -> {
            log.info("**** 绑定的本机地址为： {}  ***************", commonConfig.getBindIp());
            log.info("**** 监控时间间隔为： {}  ***************", commonConfig.getSubmitInterval() / 1000 + "秒");
            // System.out.println("绑定的本机地址为： " + commonConfig.getBindIp() + "***************");
            SystemInfo systemInfo = new SystemInfo();
            // 设置hostname
            systemInfo.setHostname(commonConfig.getBindIp());
            // 向主机发送请求
            String url = commonConfig.getServerUrl() + "/wgcloud/host/saveSystemInfo";
            // 创建 ObjectMapper 对象
            ObjectMapper objectMapper = new ObjectMapper();
            // 将实体类转换为 JSON 字符串
            String jsonStringSystemInfo = objectMapper.writeValueAsString(systemInfo);
            JSONObject resp = httpClientUtil.sendPost(url, jsonStringSystemInfo);
            if (!resp.getString("msg").contains("已经存在")) {
                log.info("主机信息推送成功");
            } else {
                log.info("主机信息已经存在，无需重复推送");
            }
        };
    }
}
